<html>
<head>
<script>

var ws = null;
var msg_id = 0;

function connect() {
    if (ws !== null) return log('already connected');
    ws = new WebSocket('ws://127.0.0.1:8088/test');
    //ws = new WebSocket('ws://127.0.0.1:8088/websocket');
    //ws = new WebSocket('ws://115.28.88.113:8088/test');
    //115.28.88.113
    //ws = new WebSocket('ws://172.17.0.2:8088/test');
    ws.onopen = function () {
        log('connected');
    };
    ws.onerror = function (error) {
        log(error);
    };
    ws.onmessage = function (e) {
        log('recv: ' + e.data);
    };
    ws.onclose = function () {
        log('disconnected');
        ws = null;
    };
    return false;
}

function disconnect() {
    if (ws === null) return log('already disconnected');
    ws.close();
    return false;
}

function count() {
    if (ws === null) return log('please connect first');
    var text = document.getElementById('text').value;
    document.getElementById('text').value = "";
    log('count ranklist...');

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.Count",
        "ranklist": "myzset",
        "session_id": "44cb892f7ca141b6df7612e284feec27"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function Rem() {
    if (ws === null) return log('please connect first');
    var text = document.getElementById('text').value;
    document.getElementById('text').value = "";
    log('remove: '+text);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.ReMoVe", 
        "key": text,
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function score() {
    if (ws === null) return log('please connect first');
    var text = document.getElementById('text').value;
    document.getElementById('text').value = "";
    log('get score: '+text);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.SCORE", 
        "key": text,
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function GetScoreRange() {
    if (ws === null) return log('please connect first');
    var lower= document.getElementById('lower').value;
    var upper= document.getElementById('upper').value; 
    document.getElementById('lower').value = "";
    document.getElementById('upper').value= "";

    log('get score range: '+ lower + ", " + upper);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.GEtSCoreRange", 
        "min": lower, 
        "max": upper, 
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function GetRank() {
    if (ws === null) return log('please connect first');
    var text = document.getElementById('text').value;
    document.getElementById('text').value = "";
    log('get rank: '+text);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.GetRAnk", 
        "key": text,
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function GetRevRank() {
    if (ws === null) return log('please connect first');
    var text = document.getElementById('text').value;
    document.getElementById('text').value = "";
    log('get rev rank: '+text);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.GETRevRanK", 
        "key": text,
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function GetRankRange() {
    if (ws === null) return log('please connect first');
    var lower= document.getElementById('lower').value;
    var upper= document.getElementById('upper').value; 
    document.getElementById('lower').value = "";
    document.getElementById('upper').value= "";

    log('get rank range: '+ lower + ", " + upper);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.getrankrange", 
        "offset": lower, 
        "count": upper, 
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function GetRevRankRange() {
    if (ws === null) return log('please connect first');
    var lower= document.getElementById('lower').value;
    var upper= document.getElementById('upper').value; 
    document.getElementById('lower').value = "";
    document.getElementById('upper').value= "";

    log('get rev rank range: '+ lower + ", " + upper);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.GetRevRankRange", 
        "offset": lower, 
        "count": upper,
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function Limit() {
    if (ws === null) return log('please connect first');
    var text = document.getElementById('text').value;
    document.getElementById('text').value = "";
    log('limit : '+text);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.LIMIT", 
        "count": text,
        "ranklist":"myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function RevLimit() {
    if (ws === null) return log('please connect first');
    var text = document.getElementById('text').value;
    document.getElementById('text').value = "";
    log('rev limit : '+text);

    var data = {
        "_msgid": msg_id, 
        "action": "RankList.REVLiMit", 
        "count": text,
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    return false;
}

function send() {
    if (ws === null) return log('please connect first');
    var text = document.getElementById('text').value;
    document.getElementById('text').value = "";
    log('send: ' + text);


    var data = {
        "_msgid": msg_id, 
        "action": "RankList.Add", 
        "key": text, 
        "value": msg_id,
        "ranklist": "myzset"
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1
        return false;
}

function log(text) {
    var li = document.createElement('li');
    li.appendChild(document.createTextNode(text));
    document.getElementById('log').appendChild(li);
    return false;
}

</script>

</head>
<body>
  <form onsubmit="return send();">
    <button type="button" onclick="return connect();">
      Connect
    </button>
    <button type="button" onclick="return disconnect();">
      Disconnect
    </button>
    key: <input id="text" type="text">
    lower_bound: <input id="lower" type="text">
    upper_bound: <input id="upper" type="text">

    <button type="submit">Add</button>
  </form>
  <form onsubmit="return count();">
      <button type="submit">Count</button>
  </form>

  <form onsubmit="return Rem();">
      <button type="submit">Remove</button>
  </form>

  <form onsubmit="return score();">
      <button type="submit">Score</button>
  </form>

  <form onsubmit="return GetScoreRange();">
      <button type="submit">ScoreRange</button>
  </form>

  <form onsubmit="return GetRank();">
      <button type="submit">Rank</button>
  </form>

<form onsubmit="return GetRevRank();">
      <button type="submit">RevRank</button>
  </form>

<form onsubmit="return GetRankRange();">
      <button type="submit">RankRange</button>
  </form>

<form onsubmit="return GetRevRankRange();">
      <button type="submit">RevRankRange</button>
  </form>

<form onsubmit="return Limit();">
      <button type="submit">Limit</button>
  </form>

<form onsubmit="return RevLimit();">
      <button type="submit">RevLimit</button>
  </form>

  <ol id="log"></ol>
</body>
</html>
